//
//  Tabbar_VC.m
//  LittlePig
//
//  Created by 达联 on 2018/6/26.
//  Copyright © 2018年 Riber. All rights reserved.
//

#import "Tabbar_VC.h"
#import "Index_VC.h"
#import "PublicMothed.h"
#import "NewMessage_VC.h"
#import "MyInfo_VC.h"

@interface Tabbar_VC () {
    UITabBarItem *_selectItem;
}

@end

@implementation Tabbar_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.viewControllers = @[[self homePage],[self newMessage],[self myInfo]];
    
}



- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    if (_selectItem != item) {
        NSInteger index = [self.tabBar.items indexOfObject:item];
        
        NSMutableArray *_tabbarViewArr = [NSMutableArray new];
        for (UIView *tempView in self.tabBar.subviews) {
            if ([tempView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                [_tabbarViewArr addObject:tempView];
            }
        }
        NSArray *sortArr = [_tabbarViewArr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            UIView *tempView1 = obj1;
            UIView *tempView2 = obj2;
            if (VIEW_X(tempView1) < VIEW_X(tempView2)) {
                return NSOrderedAscending;
            } else {
                return NSOrderedDescending;
            }
        }];
        
        [self showTabbarTouchAnimation:sortArr andIndex:index];
    }
    _selectItem = item;
}
/**
 * 执行动画效果
 */
- (void)showTabbarTouchAnimation:(NSArray *)btnArray andIndex:(NSInteger)index {
    //放大效果，并回到原位
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    //速度控制函数，控制动画运行的节奏
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration = 0.2;       //执行时间
    animation.repeatCount = 1;      //执行次数
    animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
    animation.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
    animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
    [[btnArray[index] layer] addAnimation:animation forKey:nil];
}



/**
 * 首页
 */
- (UINavigationController *)homePage {
    //item1
    static UINavigationController *homePage = nil;
    if (!homePage) {
        Index_VC *VC = [[Index_VC alloc] init];
        [VC setTitle:@"首页"];
        homePage = [[UINavigationController alloc] initWithRootViewController:VC];
        homePage.navigationBar.barStyle = UIBarStyleBlack;
        [homePage.navigationBar setTintColor:[UIColor whiteColor]];
        [homePage.navigationBar setBackgroundImage:[PublicMothed getImgWithColor:BLUE] forBarMetrics:UIBarMetricsDefault];
        [homePage.tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} forState:UIControlStateNormal];
        [homePage.tabBarItem setImage:[[UIImage imageNamed:@"index_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [homePage.tabBarItem setSelectedImage:[[UIImage imageNamed:@"index_select"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }
    return homePage;
}
/**
 * 看点
 */
- (UINavigationController *)newMessage {
    //item1
    static UINavigationController *newMessage = nil;
    if (!newMessage) {
        NewMessage_VC *VC = [[NewMessage_VC alloc] init];
        [VC setTitle:@"看点"];
        newMessage = [[UINavigationController alloc] initWithRootViewController:VC];
        newMessage.navigationBar.barStyle = UIBarStyleBlack;
        [newMessage.navigationBar setTintColor:[UIColor whiteColor]];
        [newMessage.navigationBar setBackgroundImage:[PublicMothed getImgWithColor:BLUE] forBarMetrics:UIBarMetricsDefault];
        [newMessage.tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} forState:UIControlStateNormal];
        [newMessage.tabBarItem setImage:[[UIImage imageNamed:@"newMessage_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [newMessage.tabBarItem setSelectedImage:[[UIImage imageNamed:@"newMessage_select"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }
    return newMessage;
}
/**
 * 我的
 */
- (UINavigationController *)myInfo {
    //item1
    static UINavigationController *myInfo = nil;
    if (!myInfo) {
        MyInfo_VC *VC = [[MyInfo_VC alloc] init];
        [VC setTitle:@"我的"];
        myInfo = [[UINavigationController alloc] initWithRootViewController:VC];
        myInfo.navigationBar.barStyle = UIBarStyleBlack;
        [myInfo.navigationBar setTintColor:[UIColor whiteColor]];
        [myInfo.navigationBar setBackgroundImage:[PublicMothed getImgWithColor:BLUE] forBarMetrics:UIBarMetricsDefault];
        [myInfo.tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} forState:UIControlStateNormal];
        [myInfo.tabBarItem setImage:[[UIImage imageNamed:@"myInfo_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [myInfo.tabBarItem setSelectedImage:[[UIImage imageNamed:@"myInfo_select"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }
    return myInfo;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
